﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web;
using V5_EaspLibs;
using System.Reflection;

namespace V5_FrameWork.Data {
    /// <summary>
    /// 
    /// </summary>
    public class BaseToolsHelper {
        private static readonly BaseToolsHelper m_Instance = new BaseToolsHelper();
        /// <summary>
        /// 
        /// </summary>
        public static BaseToolsHelper Instance {
            get { return m_Instance; }
        }
        /// <summary>
        /// 加载样式
        /// </summary>
        /// <param name="p"></param>
        /// <param name="cssPath"></param>
        /// <returns></returns>
        public string Css(Page p, string cssPath) {
            string c = p.ResolveUrl("~/") + cssPath;
            return @"<link href=""" + c + @""" rel=""stylesheet"" type=""text/css"" />";
        }
        /// <summary>
        /// 加载样式
        /// </summary>
        /// <param name="p"></param>
        /// <param name="cssPath"></param>
        /// <returns></returns>
        public string Css(UserControl p, string cssPath) {
            string c = p.ResolveUrl("~/") + cssPath;
            return @"<link href=""" + c + @""" rel=""stylesheet"" type=""text/css"" />";
        }
        /// <summary>
        /// 加载脚本
        /// </summary>
        /// <param name="p"></param>
        /// <param name="jsPath"></param>
        /// <returns></returns>
        public string Script(Page p, string jsPath) {
            string c = p.ResolveUrl("~/") + jsPath;
            return @"<script src=""" + c + @""" type=""text/javascript""></script>";
        }
        /// <summary>
        /// 加载脚本
        /// </summary>
        /// <param name="p"></param>
        /// <param name="jsPath"></param>
        /// <returns></returns>
        public string Script(UserControl p, string jsPath) {
            string c = p.ResolveUrl("~/") + jsPath;
            return @"<script src=""" + c + @""" type=""text/javascript""></script>";
        }

        public string LoadAdminCss(Page p) {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine(Css(p, "includes/css/admin/default/sys_css.css"));
            sb.AppendLine(Css(p, "Scripts/jquery/jquery-ui/cupertino/jquery-ui.css"));
            return sb.ToString();
        }

        public string LoadAdminScripts(Page p) {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine(Script(p, "scripts/jquery.js"));
            sb.AppendLine(Script(p, "Scripts/jquery/jquery-ui/jquery-ui.js"));
            sb.AppendLine(Script(p, "Scripts/jquery/jquery-validate/jquery.validate.js"));
            sb.AppendLine(Script(p, "Scripts/jquery/ColorPicker/jQuery.ColorPicker.js"));
            sb.AppendLine(Script(p, "Includes/ckeditor/ckeditor.js"));
            sb.AppendLine(Script(p, "Admin/Scripts/Admin.js"));
            return sb.ToString();
        }

        /// <summary>
        /// 配置网站静态功能
        /// </summary>
        public string LoadConfigHtml() {
            return @"<span style='font-size:12px;'>请在配置文件中开启静态!</span>";
        }

        #region Decode
        /// <summary>
        /// Url地址解码
        /// </summary>
        /// <param name="pageContent"></param>
        /// <returns></returns>
        public string UrlDecode(string pageContent) {
            return System.Web.HttpUtility.UrlDecode(pageContent);
        }
        /// <summary>
        /// Url地址编码
        /// </summary>
        /// <param name="pageContent"></param>
        /// <returns></returns>
        public string UrlEncode(string pageContent) {
            return System.Web.HttpUtility.UrlEncode(pageContent);
        }
        /// <summary>
        /// Html解码
        /// </summary>
        /// <param name="pageContent"></param>
        /// <returns></returns>
        public string HtmlDecode(string pageContent) {
            return System.Web.HttpUtility.HtmlDecode(pageContent);
        }
        /// <summary>
        /// Html编码
        /// </summary>
        /// <param name="pageContent"></param>
        /// <returns></returns>
        public string HtmlEncode(string pageContent) {
            return System.Web.HttpUtility.HtmlEncode(pageContent);
        }
        #endregion

        /// <summary>
        /// 获取网页代码
        /// </summary>
        /// <param name="url">网页地址</param>
        /// <param name="pageCode">网页编码</param>
        /// <returns></returns>
        public string GetHttpPage(string url, string pageCode) {
            return Easp.Http.GetHttpPage(url, 15000, pageCode);
        }
        /// <summary>
        /// 分割字符串字符 ,
        /// </summary>
        /// <param name="strSplit"></param>
        /// <param name="strChar"></param>
        /// <returns></returns>
        public string[] Split(string strSplit, string strChar) {
            string[] ss = strSplit.Split(new string[] { strChar }, StringSplitOptions.None);
            return ss;
        }

        /// <summary>
        /// 添加页面Meta信息
        /// </summary>
        /// <param name="Seokeywords">关键词</param>
        /// <param name="Seodescription">说明</param>
        /// <param name="Seohead">其它增加项</param>
        public string AddMetaInfo(string Seokeywords, string Seodescription, string Seohead) {
            StringBuilder strTxt = new StringBuilder();
            if (Seokeywords != "") {
                strTxt.Append("<meta name=\"keywords\" content=\"" + Easp.Str.CutStr(Seokeywords, 250).Replace("\"", " ") + "\" />\r\n");
            }
            if (Seodescription != "") {
                strTxt.Append("<meta name=\"description\" content=\"" + Easp.Str.CutStr(Seodescription, 250).Replace("\"", " ") + "\" />\r\n");
            }
            strTxt.Append("<link rel=\"icon\" href=\"favicon.ico\" mce_href=\"favicon.ico\" type=\"image/x-icon\"> \r\n");
            strTxt.Append("<link rel=\"shortcut icon\" href=\"favicon.ico\" mce_href=\"favicon.ico\" type=\"image/x-icon\"> \r\n");
            strTxt.Append(Seohead);
            return strTxt.ToString();
        }

        /// <summary>
        /// 检查类型中的函数是否存在
        /// </summary>
        /// <param name="Name">函数变量</param>
        /// <param name="FuncName">函数名称</param>
        /// <returns></returns>
        public bool CheckFunction(object Name, string FuncName) {
            Type t = Name.GetType();
            //PropertyInfo[] propertys = t.GetProperties();
            MethodInfo[] methods = t.GetMethods();
            foreach (MethodInfo mi in methods) {
                if (mi.Name == FuncName) {
                    return true;
                }
            }
            return false;
        }

    }
}
